import Tab, { type ITabView } from '.';
import type CardService from '../../cards/base/service';
import DI from '../../utils/di';

class TabService extends DI implements ITabView {
  title: string;
  cards: CardService[];
  component: React.FC<any> = Tab;
  constructor({ title, cards }: { title: string; cards: CardService[] }) {
    super();
    this.title = title;
    this.cards = cards;
  }

  onShow() {
    this.cards.forEach(it => { it?.onShow(); });
  }

  labelWidth: number | string = 100;

  initLableWidth() {
    this.labelWidth = this.getMaxLabelWidth() + 'px';
  }

  getMaxLabelWidth() {
    return this.cards.reduce((pre, cur) => {
      return Math.max(pre, cur.getLabelWidth());
    }, 0);
  }

  init() {
    this.initLableWidth();
    this.cards.forEach(it => {
      it.bindBpmnService(this.root);
      it.bindPanelService(this.panel);
    });
    this.cards.forEach(it => {
      it.init();
    });
  }
}

export default TabService;
